home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11215 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: po.CWRU.Edu!mab22
  2. From: mab22@po.CWRU.Edu (Michael A. Balfour)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. Date: 22 Mar 1996 17:02:31 GMT
  6. Organization: Case Western Reserve University, Cleveland, OH (USA)
  7. Message-ID: <4iumf8$604@madeline.INS.CWRU.Edu>
  8. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> <315006F8.639@cmt.lpr.mail.carel.fi>
  9. Reply-To: mab22@po.CWRU.Edu (Michael A. Balfour)
  10. NNTP-Posting-Host: kanga.ins.cwru.edu
  11.  
  12.  
  13. In a previous article, aril@cmt.lpr.mail.carel.fi (Ari Lukumies) says:
  14.  
  15. >How about something like:
  16. >
  17. >    struct {
  18. >        int    flag;
  19. >        union {
  20. >            char    text[80];
  21. >            int    num;
  22. >            double    fpoint;
  23. >        } data;
  24. >    } test;
  25. >
  26.  
  27. Wasn't the original goal of the variant records to avoid chewing up
  28. extra memory?  As I understand it, union uses as much memory as the
  29. largest member.  In this case, even if you are only storing an int, you
  30. still use 80 bytes, right?
  31.  
  32. So my answer to the poster is that there is no mechanism in C
  33. specifically designed to handle variant records using only as much
  34. memory as data.
  35.  
  36. *However*, if you feel like it, you can still do it yourself with some
  37. clever mallocs and overlaying structures.  Not exactly elegant, but you
  38. can make it pretty memory-efficient.
  39.  
  40. Mike Balfour
  41.  
  42. -- 
  43. ----------------------------------+--------------------------------
  44. Mike Balfour, Partner             | BS/MS Graduate - ECMP
  45. Overload Engineering              | Case Western Reserve University
  46. "New Ideas for a Brighter Future" | Cleveland, OH
  47.